package com.yic.module.base.service.unit;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yic.framework.common.util.collection.CollectionUtils;
import com.yic.module.base.dal.redis.RedisKeyConstants;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.base.controller.admin.unit.vo.*;
import com.yic.module.base.dal.dataobject.unit.UnitDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.base.convert.unit.UnitConvert;
import com.yic.module.base.dal.mysql.unit.UnitMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.base.enums.ErrorCodeConstants.*;

/**
 * 单位 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class UnitServiceImpl implements UnitService {

    @Resource
    private UnitMapper unitMapper;

    @Override
    public Long createUnit(UnitCreateReqVO createReqVO) {
        // 校验名称是否已经存在
        validateUnitNameExists(createReqVO.getName());
        //校验单位代码是否已经存在
        validateUnitCodeExists(createReqVO.getCode());
        // 插入
        UnitDO unit = UnitConvert.INSTANCE.convert(createReqVO);
        unitMapper.insert(unit);
        // 返回
        return unit.getId();
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.UNIT, key = "#updateReqVO.id")
    public void updateUnit(UnitUpdateReqVO updateReqVO) {
        // 校验
        UnitDO unit = this.getUnit(updateReqVO.getId());
        if (ObjectUtil.isEmpty(unit)){
            throw exception(UNIT_NOT_EXISTS);
        }
        if (!updateReqVO.getName().equals(unit.getName())){
            // 单位名称已经发生改变，需要校验改变后的名称是否在系统中已经存在
            validateUnitNameExists(updateReqVO.getName());
        }
        if (!updateReqVO.getCode().equals(unit.getName())){
            // 单位代码已经发生改变，需要校验改变后的代码是否在系统中已经存在
            validateUnitCodeExists(updateReqVO.getCode());
        }
        // 更新
        UnitDO updateObj = UnitConvert.INSTANCE.convert(updateReqVO);
        unitMapper.updateById(updateObj);
    }

    @Override
    @CacheEvict(cacheNames = RedisKeyConstants.UNIT, key = "#id")
    public void deleteUnit(Long id) {
        // 校验存在
        validateUnitExists(id);
        // 删除
        unitMapper.deleteById(id);
    }

    private void validateUnitExists(Long id) {
        if (unitMapper.selectById(id) == null) {
            throw exception(UNIT_NOT_EXISTS);
        }
    }

    private void validateUnitNameExists(String name) {
        if (ObjectUtil.isNotEmpty(unitMapper.selectByName(name))){
            throw exception(UNIT_NAME_EXISTS);
        }
    }

    private void validateUnitCodeExists(String code) {
        if (ObjectUtil.isNotEmpty(unitMapper.selectByCode(code))){
            throw exception(UNIT_CODE_EXISTS);
        }
    }

    @Override
    @Cacheable(cacheNames = RedisKeyConstants.UNIT, key = "#id", unless = "#result == null")
    public UnitDO getUnit(Long id) {
        return unitMapper.selectById(id);
    }

    @Override
    public List<UnitDO> getUnitList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)){
            return Collections.emptyList();
        }
        return unitMapper.selectBatchIds(ids);
    }

    @Override
    public List<UnitDO> getUnitList() {
        return unitMapper.selectList();
    }

    @Override
    public PageResult<UnitDO> getUnitPage(UnitPageReqVO pageReqVO) {
        return unitMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UnitDO> getUnitList(UnitExportReqVO exportReqVO) {
        return unitMapper.selectList(exportReqVO);
    }

    @Override
    public Map<Long, UnitDO> getUnitMap(Collection<Long> ids) {
        if(CollUtil.isEmpty(ids)){
            return Collections.emptyMap();
        }
        List<UnitDO> list = this.getUnitList(ids);
        return CollectionUtils.convertMap(list, UnitDO::getId);
    }

    @Override
    public UnitDO getUnitByErpId(String erpUnitId) {
        return unitMapper.selectByErpId(erpUnitId);
    }

    @Override
    public List<UnitDO> getUnitListByErpId(Collection<String> erpUnitIds) {
        return unitMapper.selectListByErpId(erpUnitIds);
    }


}
